const app = getApp()
Page({
  data: {
    show_follow:true,
    disabled:false,
    onDate:false,
    onSelector:false,
    minDate: new Date().getTime(),
    maxDate: new Date(2030,1,1).getTime(),
    currentDate: new Date().getTime(),
    telphone:'',
    message:'',
    item:{},
    fields:[],
    id:0
  },
  //监听页面初次载入完成
  onLoad: function (options) {
    this.data.id = options.id
    this.setData({
      uid:app.globalData.loginuser.uid
    })
  },
  //监听页面初次载入完成
  onShow: function () {
    app.loginState((rel)=>{
      this.getView();
    },true)
  },
  //获取首页接口
  getView: function () {
    app.api().Get('api/v1/ais/info/getview',{info_id:this.data.id},(result)=>{
      this.setData({
        item: result.data,
        fields: result.data.users,
      });
    })
  },
  //提交订单
  onSubmit: function(e){
    this.setData({
      disabled:true
    })
     if (app.util().isNull(this.data.telphone) || !(/^1(2|3|4|5|6|7|8|9)\d{9}$/.test(this.data.telphone))){
      app.wxAlert('手机号码不正确');
      this.setData({
        disabled:false
      })
      return;
    }
    app.api().Post('api/v1/ais/infopay/index', {
      telphone:this.data.telphone,
      message:this.data.message,
      id:this.data.id,
      ucode: app.globalData.ucode,
      fields:this.fields(),
    },(res) => {
      if (res.code == 200) {
       app.doWechatPay(res.data,()=>{
          wx.redirectTo({
            url: 'user/index?active=2',
          })
       },()=>{
        wx.showModal({
          title:'微信支付',content:'你已取消支付,是否重试',
          cancelText:'取消',confirmText	:'重试',
          success:(res) => {
            if (res.confirm) {
              this.onSubmit();
            }else{
              this.setData({
                disabled:false
              })
            }
          }
        })
       })
      }
    })
  },
  //关注与取消
  onFollow:function (e) {
    var item = this.data.item;
    if (item.is_follow == 0){
      app.api().Post("api/v1/ais/infosend/follow",{uid:item.user_id,state:1},(rel) => {
        item.is_follow = 1;
        this.setData({
          item:item
        })
      })
    }
  },
  //动态表单数据
  fields:function () {
    var fields = this.data.fields;
    var field = new Array();
    for (let i in fields) {
      if(app.util().isNull(fields[i].value)){
        var value = {title:fields[i].title,value:''}
      }else{
        var value = {title:fields[i].title,value:fields[i].value}
      }
      field.push(value)
    }
    return JSON.stringify(field);
  },
  //保存表单值
  onIpunt: function (e) {
    var key = e.currentTarget.dataset.id;
    var fields = this.data.fields;
       fields[key].value = e.detail;
  },
  //时间选择器
  selectDate: function (e) {
    this.setData({
      onDate:!this.data.onDate
    });
    if(this.data.onDate){
      this.data.field_key = e.currentTarget.dataset.id;
    }
  },
  onDateConfirm: function (event) {
    var nowdate = app.util().dateFormat(event.detail,'Y/M/D h:m')
    var fields = this.data.fields;
        fields[this.data.field_key].value = nowdate; 
    this.setData({
      fields:fields
    });
    this.selectDate();
  },
  //菜单选择器
  selector: function (e) {
    this.setData({
      onSelector: !this.data.onSelector
    });
    if(this.data.onSelector){
      this.data.field_key = e.currentTarget.dataset.id;
      this.setData({
        columns:this.data.fields[this.data.field_key].values
      })
    }
  },
  onSelector: function (event) {
    var fields = this.data.fields;
        fields[this.data.field_key].value = event.detail.value; 
    this.setData({
      fields:fields
    })
    this.selector();
  },
  //保存表单值
  telphone: function(e) {
    this.data.telphone = e.detail;
  },
  message: function(e) {
    this.data.message = e.detail;
  }
})